package com.zjdiepu.www.manage.api.dh;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.net.SocketTimeoutException;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.lang.HttpClientException;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.dh.IDhLoanAfterService;
import com.zjdiepu.www.ibiz.third.net.IDeliverService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.dh.dto.DhLoanAfter;
import com.zjdiepu.www.model.dh.ext.DhLoanAfterExt;
import com.zjdiepu.www.model.third.request.ali.AliDeliverRequest;
import com.zjdiepu.www.model.third.response.ali.AliDeliverResponseForShow;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 贷后信息登记Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月10日 14时56分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "DhLoanAfterController", produces="application/json;charset=UTF-8")
@Api(tags = {"贷后信息登记"}, produces="application/json;charset=UTF-8")
public class DhLoanAfterController extends BaseController {
    @Resource
    private IDhLoanAfterService dhLoanAfterService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private YwBusinessVoMapper ywBusinessVoMapper;
    
    @Resource(name="aliDeliverServiceImpl")
    private IDeliverService<AliDeliverRequest, AliDeliverResponseForShow> deliverService;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: queryExpress  <br/>
     * @Description: 删除逾期_快递状态<br/>
     * @param nu
     * @param com
     * @return
     * @version V1.0  <br/>
     * @throws HttpClientException 
     * @throws SocketTimeoutException 
     */
    @ApiOperation(value = "快递运单号", response = Boolean.class, httpMethod="POST", tags={"贷后信息登记"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "nu", value = "快递运单号", paramType="query", required = true, example="124234233"),
        @ApiImplicitParam(name = "com", value = "快递公司编号", paramType="query", required = false, example="zhongtong")
    })
    @RequestMapping(value = "queryExpress", method = RequestMethod.POST)
    public ApiResponse queryExpress(@RequestParam(required = true)String nu, String com) {
    	if (StringUtils.isBlank(nu)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	try {
			AliDeliverResponseForShow saveExpInfo = deliverService.saveExpInfo(nu, com);
			return ApiResponse.success(saveExpInfo);
		} catch (SocketTimeoutException | HttpClientException e) {
			return ApiResponse.fail(999,"获取第三方快递数据为空");
		}
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: loanAfter  <br/>
     * @Description: 保存贷后信息 <br/>
     * @param dhLoanAfter
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "贷后信息登记", response = Boolean.class, httpMethod="POST", tags={"贷后信息登记"})
    @RequestMapping(value = "loanAfter", method = RequestMethod.POST)
    public ApiResponse loanAfter(@Validated(Add.class) @ModelAttribute DhLoanAfter dhLoanAfter) {
    	AbstractSysUser currentUser = getCurrentUser();
        return dhLoanAfterService.saveLoanAfter(dhLoanAfter, currentUser);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: loanAfterList <br/>
     * @Description: 精确定位客户贷后信息登记 <br/>
     * @param ywBusinessExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "精确定位客户贷后信息登记 ",response = YwBusiness.class, httpMethod="POST", tags={"贷后信息登记"})
    @RequestMapping(value = "loanAfterList", method = RequestMethod.POST)
    public ApiResponse loanAfterList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser =(SysUser)getCurrentUser();
    	PageVo page = getPage(ywBusinessQueryRequest);
    	ywBusinessQueryRequest.setBaseWhere();
    	ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
//    	业务状态为“银行已放款”或者“还款中”或者“逾期”
//    	ywBusinessQueryRequest.addBusiStatus(BusiStatus.YIN_HANG_YI_FANG_KUAN.getCode());
//      ywBusinessQueryRequest.addBusiStatus(BusiStatus.HUAI_KUAN_ZHONG.getCode());
//    	ywBusinessQueryRequest.setOrgId(currentUser.getOrgId());
//    	ywBusinessQueryRequest.setCodeUnits(currentUser.getCodeUnits());
    	ywBusinessQueryRequest.setMinBusiStatus(BusiStatus.SHEN_PI_WAN_CHENG.getCode());
        ywBusinessQueryRequest.setMaxBusiStatus(BusiStatus.YIN_HANG_YI_FANG_KUAN.getCode());
        ywBusinessQueryRequest.setDh(true);
    	int records = ywBusinessService.countLessOptionsByExt(ywBusinessQueryRequest);
    	page.setRecords(records);
    	if (records > 0) {
    		List<YwBusiness> ywBusinesss = ywBusinessService.selectLessOptionsByExt(ywBusinessQueryRequest);
    		ywBusinessVoMapper.mapper(ywBusinesss);
    		page.setRows(ywBusinesss);
    	} else {
    		page.setRows(CollectionInitUtil.emptyList());
    	}
    	return ApiResponse.success(page);
    }
    

    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 新增贷后信息登记 <br/>
     * @param dhLoanAfter
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增贷后信息登记", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute DhLoanAfter dhLoanAfter) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dhLoanAfter.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhLoanAfter.setModifierName(currentUser.getName());
        dhLoanAfterService.saveUpdateAndDelete(dhLoanAfter, OperateType.add);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: remove  <br/>
     * @Description: 删除贷后信息登记<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除贷后信息登记", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhLoanAfterService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: disable  <br/>
     * @Description: 禁用贷后信息登记<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用贷后信息登记", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhLoanAfterService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: enable <br/>
     * @Description: 启用贷后信息登记<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用贷后信息登记", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhLoanAfterService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: modify  <br/>
     * @Description: 修改贷后信息登记 <br/>
     * @param dhLoanAfter
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改贷后信息登记", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute DhLoanAfter dhLoanAfter) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dhLoanAfter.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhLoanAfter.setModifierName(currentUser.getName());
    	dhLoanAfterService.saveUpdateAndDelete(dhLoanAfter, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询贷后信息登记<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询贷后信息登记", response = DhLoanAfter.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       DhLoanAfter dhLoanAfter = dhLoanAfterService.selectById(id);
       if(null == dhLoanAfter) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(dhLoanAfter);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询贷后信息登记 <br/>
     * @param dhLoanAfterExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询贷后信息登记",response = DhLoanAfter.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute DhLoanAfterExt dhLoanAfterExt){
        dhLoanAfterExt.notDeleted();
        PageVo page = getPage(dhLoanAfterExt);
        int records = dhLoanAfterService.countByExt(dhLoanAfterExt);
        page.setRecords(records);
        if (records > 0) {
            List<DhLoanAfter> dhLoanAfters = dhLoanAfterService.selectByExt(dhLoanAfterExt);
            page.setRows(dhLoanAfters);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

}
